package com.jsh.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jsh.erp.datasource.entities.BomMaterial;
import com.jsh.erp.datasource.vo.BomMaterialAddParam;
import com.jsh.erp.datasource.vo.BomMaterialSaveParam;
import com.jsh.erp.datasource.vo.BomMaterialSearchParam;
import com.jsh.erp.service.bom.BomService;
import com.jsh.erp.service.bomMaterial.BomMaterialService;
import com.jsh.erp.web.vo.BaseResponseInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author chenzipeng
 */
@RestController
@RequestMapping(value = "/bomMaterial")
@Api(tags = {"Bom原料管理"})
public class BomMaterialController {
    private Logger logger = LoggerFactory.getLogger(BomMaterialController.class);

    @Resource
    private BomService bomService;

    @Resource
    private BomMaterialService bomMaterialService;


    @ApiOperation("查询")
    @PostMapping("search")
    public BaseResponseInfo<IPage<BomMaterial>> find(@RequestBody BomMaterialSearchParam searchParam) {
        QueryWrapper<BomMaterial> queryWrapper = new QueryWrapper();
        String keyword = searchParam.getKeyword();
        queryWrapper = queryWrapper.eq("bom_id", searchParam.getBomId())
                .like("name", keyword)
                .or()
                .like("bar_code", keyword);
        IPage<BomMaterial> pageResult = bomMaterialService.page(searchParam.getPageable(), queryWrapper);
        return BaseResponseInfo.success(pageResult);
    }

    @ApiOperation("查询Bom的原料列表")
    @PostMapping("getListForBom")
    public BaseResponseInfo<List<BomMaterial>> getListForBom(String keyword, Long bomId) {
        QueryWrapper<BomMaterial> queryWrapper = new QueryWrapper();
        queryWrapper = queryWrapper.eq("bom_id", bomId)
                .like("name", keyword)
                .or()
                .like("bar_code", keyword);
        List<BomMaterial> result = bomMaterialService.list(queryWrapper);
        return BaseResponseInfo.success(result);
    }


    @ApiOperation("获取详情")
    @GetMapping("get")
    public BaseResponseInfo<BomMaterial> get(Long id) {
        BomMaterial bomMaterial = bomMaterialService.getById(id);
        return BaseResponseInfo.success(bomMaterial);
    }

    @ApiOperation("保存")
    @PostMapping("save")
    public BaseResponseInfo<BomMaterial> save(@RequestBody BomMaterial bomMaterial) {
        bomMaterialService.save(bomMaterial);
        return BaseResponseInfo.success(bomMaterial);
    }

    @ApiOperation("删除")
    @DeleteMapping("delete")
    public void delete(Long id) {
        bomMaterialService.removeById(id);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("deleteBatch")
    public void deleteBatch(List<Long> ids) {
        bomMaterialService.removeByIds(ids);
    }
}
